package top.huahaizhi.mpchart.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;
import java.util.Random;

/**
 * xbo 柱状图
 */
public class BarChartActivity extends AppCompatActivity {

    private BarChart barChart;
    private DrawThread drawThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_chart);
        barChart = (BarChart) findViewById(R.id.barChart);
        drawThread = new DrawThread();
        drawHistogram();
    }

    public void drawHistogram() {
        //名称
        ArrayList<String> mMonths = new ArrayList<String>();
        mMonths.add("3月3日");
        mMonths.add("3月6日");
        mMonths.add("3月10日");
        mMonths.add("3月13日");
        mMonths.add("3月17日");
        mMonths.add("3月20日");
        mMonths.add("3月24日");
        //大小（高低）
        Random random = new Random();
        ArrayList<BarEntry> sizes = new ArrayList<BarEntry>();
        for (int i = 0; i < mMonths.size(); i++) {
            sizes.add(new BarEntry(random.nextInt(50), i));
        }
        //颜色
        BarDataSet barDataSet = new BarDataSet(sizes, "");
        barDataSet.setValueTextSize(10f);
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int i = 0; i < mMonths.size(); i++) {
            colors.add(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        }
        //颜色设置
        barDataSet.setColors(colors);
        BarData barData = new BarData(mMonths, barDataSet);
        barChart.setDescription("历史出勤统计图");//数据描述
        barChart.setDescriptionTextSize(12f);
        barChart.setNoDataTextDescription("No data for the chart");// 如果没有数据的时候，会显示这个，类似ListView的EmptyView
        barChart.setPinchZoom(false);// 集双指缩放
        barChart.setScaleEnabled(false);//手动缩放效果
        barChart.setPinchZoom(false);//xy轴同时缩放,和setScaleEnabled一起使用

        barChart.setDrawGridBackground(false);
        //barChart.setDrawBorders(false);//画布边
        //barChart.setVisibleXRange(7);
        //barChart.setMaxVisibleValueCount(6);
        barChart.setDrawBarShadow(false);//设置矩形阴影不显示
        //barChart.setBackgroundColor(Color.parseColor("#FFFFFF"));//设置背景颜色

        //barChart.setMinOffset(0);//=padding
        barChart.setDrawValueAboveBar(true);

        barChart.setData(barData);
        barChart.animateXY(1000, 1000);//设置动画
        Legend legend = barChart.getLegend();//取消图形说明
        legend.setEnabled(false);

        //获取X轴坐标
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//X坐标位于图标底部
        xAxis.setDrawGridLines(false);
        xAxis.setSpaceBetweenLabels(2);//设置名字names之间的间距
        //xAxis.

        //获取Y轴右坐标
        YAxis yAxisR = barChart.getAxisRight();
        yAxisR.setEnabled(true);
        yAxisR.setDrawGridLines(true);

        //获取Y轴左坐标
        YAxis yAxisL = barChart.getAxisLeft();
        yAxisL.setEnabled(true);
        yAxisL.setDrawGridLines(true);

        // 开启死循环
        drawThread.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                drawHistogram();
            }
        }, 3000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (drawThread != null) {
            drawThread.getHandler().removeCallbacksAndMessages(null);
            drawThread.getHandler().getLooper().quit();
            drawThread.setHandler(null);
            drawThread = null;
        }
    }
}
